package com.netblizzard.nms.collect;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.netblizzard.nms.util.database.CommonDBO;
import com.netblizzard.nms.util.database.SpringUtil;

public class MainServer {
	private static final Log logger = LogFactory.getLog(MainServer.class);

	private static CommonDBO dboper = null;
	private static List<CommonDBO> collectDboperList = null;

	private static int COLLECT_DBO_COUNT = 3;

	public static void main(String[] args) {
		logger.info("Collect Server Start ... ");

		// read the xml config file
		String xmlFile ="collect.xml";
		
			try {
				SpringUtil.init(xmlFile);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		dboper =(CommonDBO) SpringUtil.getBean("dboper");	// dataBase.xml

		initCollectDboper();
	}

	private static void initCollectDboper() {
		logger.info("init collect dboper ... ");
		collectDboperList = new ArrayList<CommonDBO>();

		for (int i = 0; i < COLLECT_DBO_COUNT; i++) {
			try {
				CommonDBO dbo = (CommonDBO) dboper.clone();
				while (!initDBO(dbo)) {
					logger.info("database connect fail -- " + i +"; wait 60 seconds");
					Thread.sleep(60000);
				}
				logger.info("database connect success -- " + i);
				collectDboperList.add(dbo);
			} catch (Exception e) {
				logger.error(e.getMessage());
			}
		}

	}

	private static boolean initDBO(CommonDBO dbo) {
		return dbo.connect();
	}

}
